/**	
 * タイトル：	
 * 説明：    	
 *			
 * 会社名：	紹興日創軟件有限公司
 * 変更履歴：	2014/08/13 NIS 何
 *			新規作成
 *
 */
package com.nissoft.test;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nissoft.framework.exception.FileUploadException;
import com.nissoft.framework.message.MessageReader;
import com.nissoft.framework.web.ContentType;
import com.nissoft.framework.web.upload.FileUploader;
import com.nissoft.framework.web.upload.UploadCallback;
import com.nissoft.framework.web.upload.UploadFile;

/**
 * @author NIS 何
 * @version 1.0
 */
@Controller
public class MyController {

	@Resource(name = "sqlReader")
	MessageReader text;

	@Resource(name = "propReader")
	MessageReader prop;

	@Resource(name = "xmlReader")
	MessageReader xml;

	@Resource(name = "uploader")
	FileUploader uploader;

	@RequestMapping(value = "/message/text", produces = "application/json;charset=utf-8;")
	@ResponseBody
	public String get(String key) throws Exception {

		return text.read(key);
	}

	@RequestMapping(value = "/message/prop", produces = ContentType.JSON)
	@ResponseBody
	public String prop(String key) throws Exception {

		return prop.read(key);
	}

	@RequestMapping(value = "/message/xml", produces = ContentType.JSON)
	@ResponseBody
	public String xml(String key) throws Exception {

		return xml.read(key);
	}

	@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = ContentType.JSON)
	@ResponseBody
	public void upload(HttpServletRequest req) {

		try {
			uploader.upload(req, new MyUploadCallback());
		} catch (FileUploadException e) {
			e.getErrCode();
			e.printStackTrace();
		}
	}

	private class MyUploadCallback extends UploadCallback {

		@Override
		public void successed(UploadFile uploadFile) {
			System.out.println(uploadFile.getFieldName());
			System.out.println(uploadFile.getAbsolutePath());
		}
	}
}
